# -*- coding:utf-8 -*-
"""
作者：ChenTao
日期：2023年05月13日
"""

# coding = utf8
import time

'''
Created on 2016-7-4
@desc: change the network of system and reboot,shutdown ;suit for ubuntu and centos
@attention: if ubuntu, need root or sudo without password ;and before use this script ,
            you need change something; eg: 
            <
            chown root:root systemControl.py
            chmod 777 systemControl.py
            chmod 777 /etc/network/interfaces
            >
'''

import sys
import os
import platform


class SystemControl(object):

    def __init__(self):
        """
        @desc: init
        """
        self.platform = self.getPlatform()

    def shutDown(self):
        """
        @attention: shutdown the system
        """
        print(f"{'-' * 20}Shutting down{'-' * 20}")
        time.sleep(3)
        if self.platform == 'Windows':
            os.system('shutdown /s /t 0')  # Windows关机命令
        elif self.platform == 'Linux' or self.platform == 'Darwin':
            os.system(f'echo "123456" | sudo -S shutdown -h now')  # Linux和macOS关机命令

    def reboot(self):
        """
        @attention: reboot the system
        """
        print(f"{'-' * 20}Rebooting{'-' * 20}")
        time.sleep(3)
        if self.platform == 'Windows':
            os.system('shutdown /r /t 0')  # Windows关机命令
        elif self.platform == 'Linux' or self.platform == 'Darwin':
            os.system(f'echo "123456" | sudo -S shutdown -r now')  # Linux和macOS关机命令

    def delWith(self, command):
        if command == "reboot":
            self.reboot()
        elif command == "shutdown":
            self.shutDown()
        else:
            print("no this command......")

    def getPlatform(self):
        """
        @attention: get the platform of the system
        """
        system = platform.system()

        if system == 'Windows':
            print('Windows')
        elif system == 'Linux':
            print('Linux')
        elif system == 'Darwin':
            print('macOS')
        else:
            print('Unknown system')
        return system


if __name__ == "__main__":
    sys = SystemControl()
    sys.shutDown()
